<route lang="yaml">
meta:
enabled: false
</route>

<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus'
import apiPlaInfConf from '@/api/modules/plaInfConf'
import { ElMessage } from 'element-plus'
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const formRef = ref<FormInstance>()

// 当前表单数据接口
interface SysPlaInfConf {
  id: number
  information: string
  informationValue: string
  informationEn: string
  informationDescription: string
}

const formRules = ref<FormRules>({
  information: [
    { required: true, message: '请输入信息字段', trigger: 'blur' },
  ],
  informationValue: [
    { required: true, message: '请输入信息值', trigger: 'blur' },
  ],
  informationEn: [
    { required: true, message: '请输入英文值', trigger: 'blur' },
  ],
  informationDescription: [
    { required: true, message: '请输入描述', trigger: 'blur' },
  ],
})

// 表单数据
const formData = ref<SysPlaInfConf>({
  id: 0,
  information: '',
  informationValue: '',
  informationEn: '',
  informationDescription: '',
})
const loading = ref(false)

// 返回上一页
function goBack() {
  router.back()
}

// 提交修改
function onSubmit() {
  return new Promise<void>(() => {
    formRef.value?.validate((valid) => {
      if (valid) {
        if (formData.value.id === 0) {
          // 新增操作
          apiPlaInfConf.create(formData.value).then(() => {
            ElMessage.success({
              message: '新增成功',
            })
            goBack()
          })
        }
        else {
          // 编辑操作
          apiPlaInfConf.edit(formData.value).then(() => {
            ElMessage.success({
              message: '编辑成功',
            })
            goBack()
          })
        }
      }
    })
  })
}

// 加载详情数据
function loadDetail() {
  const id = Number(route.params.id)
  if (!id) {
    return
  }
  loading.value = true
  apiPlaInfConf.detail(id).then((res: any) => {
    if (res.code === 200 && res.data) {
      formData.value = res.data
    }
    else {
      ElMessage.error(res.message || '获取详情失败')
    }
    loading.value = false
  }).catch(() => {
    ElMessage.error('请求失败')
    loading.value = false
  })
}

onMounted(() => {
  loadDetail()
})
</script>

<template>
  <div class="container">
    <FaPageHeader :title="route.name === 'plaInfConfCreate' ? '新增信息配置' : '编辑信息配置'" class="mb-0">
      <ElButton size="default" round @click="goBack">
        <template #icon>
          <FaIcon name="i-ep:arrow-left" />
        </template>
        返回
      </ElButton>
    </FaPageHeader>

    <FaPageMain v-loading="loading" class="content-main">
      <ElForm ref="formRef" :model="formData" :rules="formRules" label-width="140px">
        <div class="form-row">
          <ElFormItem label="信息字段" prop="information">
            <ElInput v-model="formData.information" placeholder="请输入信息字段" />
          </ElFormItem>
        </div>

        <div class="form-row">
          <ElFormItem label="信息值" prop="informationValue">
            <ElInput v-model="formData.informationValue" placeholder="请输入信息值" />
          </ElFormItem>
        </div>

        <div class="form-row">
          <ElFormItem label="英文值" prop="informationEn">
            <ElInput v-model="formData.informationEn" placeholder="请输入英文值" />
          </ElFormItem>
        </div>

        <div class="form-row">
          <ElFormItem label="描述" prop="informationDescription">
            <ElInput v-model="formData.informationDescription" type="textarea" :rows="4" placeholder="请输入描述信息" />
          </ElFormItem>
        </div>

        <div class="form-row form-actions">
          <ElSpace>
            <ElButton @click="goBack">
              取消
            </ElButton>
            <ElButton type="primary" @click="onSubmit">
              提交
            </ElButton>
          </ElSpace>
        </div>
      </ElForm>
    </FaPageMain>
  </div>
</template>

<style scoped>
.container {
  padding: 20px;
}

.content-main {
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 5%);
}

.form-row {
  margin-bottom: 24px;
}

.el-form-item__content {
  width: 100%;
  max-width: 600px;
}

.form-actions {
  text-align: right;
}
</style>
